package com.affinity.impl.posix;

import java.util.logging.Logger;

import com.affinity.*;
import com.sun.jna.*;

/**
 * fixme: for now there is no implementation for MacOS X. <a href="http://developer.apple.com/library/mac/#releasenotes/Performance/RN-AffinityAPI/_index.html">apple dev</a>
 * tell us what MacOS X api for affinity is different from what is required
 *
 * @author cheremin
 * @since 27.04.12,  18:30
 */
public class MacOSXJNAThreadAffinityService implements ThreadAffinityService{
    private static final Logger log = Logger.getLogger( MacOSXJNAThreadAffinityService.class.getName() );


    /**
     * source:<a href="http://developer.apple.com/library/mac/#releasenotes/Performance/RN-AffinityAPI/_index.html">apple dev</a>
     *
     * @author BegemoT
     */
    private interface MachPThreadLibrary extends Library {
        public static final MachPThreadLibrary INSTANCE = ( MachPThreadLibrary ) Native.loadLibrary( "c", MachPThreadLibrary.class );

        public int pthread_self() throws LastErrorException;

        public int pthread_mach_thread_np( final int pthreadId ) throws LastErrorException;

        /**
         * <pre>
         * kern_return_t  thread_policy_set(
         *  thread_t                thread,
         *  thread_policy_flavor_t  flavor,
         *  thread_policy_t         policy_info,
         *  mach_msg_type_number_t  count);
         * </pre>
         *
         * @return
         */
        public int thread_policy_set(
                final int pthreadId,
                final int policyFlavor,
                final thread_affinity_policy policy,
                final int policiesCount
        ) throws LastErrorException;

        public int thread_policy_get(
                //fixme:
        ) throws LastErrorException;

        public class thread_affinity_policy {
            private long affinity_tag;
        }
    }

    private final MachPThreadLibrary pthreadLibrary;

    public MacOSXJNAThreadAffinityService( final CPULayoutService layoutService ) {

        pthreadLibrary = MachPThreadLibrary.INSTANCE;
    }

    @Override
    public boolean isActuallyAvailable() {
        return false;
    }

    @Override
    public void restrictCurrentThreadTo( final CPU... cpu ) {
        throw new UnsupportedOperationException( "Not implemented yet" );
    }

    @Override
    public void restrictCurrentThreadTo( final Iterable<? extends CPU> cpus ) {
        throw new UnsupportedOperationException( "Not implemented yet" );
    }

    @Override
    public Iterable<? extends CPU> currentThreadRestrictions() {
        throw new UnsupportedOperationException( "Not implemented yet" );
    }

    @Override
    public CPU currentThreadCPU() {
        throw new UnsupportedOperationException( "Not implemented yet" );
    }

}
